/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PreferenciaSocio.java
 *
 * Created on Feb 24, 2011, 1:39:34 PM
 */

package forms;

import beans.Bebida;
import beans.DiaSalida;
import beans.MedioComunicacion;
import beans.Musica;
import beans.Socio;
import dao.BebidaDao;
import dao.DiaSalidaDao;
import dao.MedioComunicacionDao;
import dao.MusicaDao;
import dao.PersonaDao;
import exceptions.DAOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import managers.ManagerSocio;


/**
 *
 * @author Guido Cresta
 */
public class PreferenciaSocio extends javax.swing.JFrame {

    private ManagerSocio gestor = new ManagerSocio();
    private Socio socio = new Socio();
    private boolean isEdit = false;

    PreferenciaSocio(Socio socio) {
        
        List<Musica> musicas = MusicaDao.getMusica();
        for(Musica musica : musicas){
            if(!socio.getPreferenciaMusica().contains(musica))
                modeloMusica.addElement(musica);
        }
        for(Musica musica : socio.getPreferenciaMusica())
            modeloMusicaPreferida.addElement(musica);



        List<Bebida> bebidas = BebidaDao.getBebidas();
        for(Bebida bebida : bebidas){
            if(!socio.getPreferenciaBebida().contains(bebida))
                modeloBebida.addElement(bebida);
        }
        for(Bebida bebida : socio.getPreferenciaBebida())
            modeloBebidaPreferida.addElement(bebida);

        List<DiaSalida> diaSalidas = DiaSalidaDao.getDiaSalida();
        for(DiaSalida diaSalida : diaSalidas){
            if(!socio.getPreferenciaDias().contains(diaSalida))
                modeloDiasSalida.addElement(diaSalida);
        }
        for(DiaSalida dia : socio.getPreferenciaDias())
            modeloDiasSalidaPreferida.addElement(dia);

        List<MedioComunicacion> medioComunicaciones = MedioComunicacionDao.getMedioComunicacion();
        for(MedioComunicacion medioComunicacion : medioComunicaciones){
            if(!socio.getPreferenciaComunicacion().contains(medioComunicacion))
                modeloComunicacion.addElement(medioComunicacion);
        }
        for(MedioComunicacion medio : socio.getPreferenciaComunicacion())
            modeloComunicacionPreferida.addElement(medio);
         

        initComponents();
    }

    public boolean isIsEdit() {
        return isEdit;
    }

    public void setIsEdit(boolean isEdit) {
        this.isEdit = isEdit;
    }

    public Socio getSocio() {
            return socio;
    }

    public void setSocio(Socio socio) {
            this.socio = socio;
    }

    /** Creates new form PreferenciaSocio */
    public PreferenciaSocio() {
        List<Musica> musicas = MusicaDao.getMusica();
        for(Musica musica : musicas)
            modeloMusica.addElement(musica);

        List<Bebida> bebidas = BebidaDao.getBebidas();
        for(Bebida bebida : bebidas)
            modeloBebida.addElement(bebida);

        List<DiaSalida> diaSalidas = DiaSalidaDao.getDiaSalida();
        for(DiaSalida diaSalida : diaSalidas)
            modeloDiasSalida.addElement(diaSalida);

        List<MedioComunicacion> medioComunicaciones = MedioComunicacionDao.getMedioComunicacion();
        for(MedioComunicacion medioComunicacion : medioComunicaciones)
            modeloComunicacion.addElement(medioComunicacion);

        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    //@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btn_agregar = new javax.swing.JButton();
        label1 = new java.awt.Label();
        jScrollPane1 = new javax.swing.JScrollPane();
        list_left = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        list_add = new javax.swing.JList();
        label2 = new java.awt.Label();
        jScrollPane6 = new javax.swing.JScrollPane();
        lista_bebida_add = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane8 = new javax.swing.JScrollPane();
        list_diaSal_left = new javax.swing.JList();
        jScrollPane9 = new javax.swing.JScrollPane();
        list_diaSal_add = new javax.swing.JList();
        jScrollPane3 = new javax.swing.JScrollPane();
        list_bebida_left = new javax.swing.JList();
        jScrollPane4 = new javax.swing.JScrollPane();
        list_medioCom_left = new javax.swing.JList();
        jScrollPane7 = new javax.swing.JScrollPane();
        lista_medioCom_add = new javax.swing.JList();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        btn_agregar.setText("Agregar");
        btn_agregar.setActionCommand("Siguiente");
        btn_agregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_agregarActionPerformed(evt);
            }
        });

        label1.setText("Preferencias Socios");

        list_left.setModel(modeloMusica);
        list_left.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                list_leftMousePressed(evt);
            }
        });
        jScrollPane1.setViewportView(list_left);

        list_add.setModel(modeloMusicaPreferida);
        list_add.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                list_addMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(list_add);

        label2.setText("Música");

        lista_bebida_add.setModel(modeloBebidaPreferida);
        lista_bebida_add.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lista_bebida_addMouseClicked(evt);
            }
        });
        jScrollPane6.setViewportView(lista_bebida_add);

        jLabel1.setText("Días de Salida");

        jLabel2.setText("Bebida");

        list_diaSal_left.setModel(modeloDiasSalida);
        list_diaSal_left.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                list_diaSal_leftMouseClicked(evt);
            }
        });
        jScrollPane8.setViewportView(list_diaSal_left);

        list_diaSal_add.setModel(modeloDiasSalidaPreferida);
        list_diaSal_add.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                list_diaSal_addMouseClicked(evt);
            }
        });
        jScrollPane9.setViewportView(list_diaSal_add);

        list_bebida_left.setModel(modeloBebida);
        list_bebida_left.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                list_bebida_leftMousePressed(evt);
            }
        });
        jScrollPane3.setViewportView(list_bebida_left);

        list_medioCom_left.setModel(modeloComunicacion);
        list_medioCom_left.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                list_medioCom_leftMousePressed(evt);
            }
        });
        jScrollPane4.setViewportView(list_medioCom_left);

        lista_medioCom_add.setModel(modeloComunicacionPreferida);
        lista_medioCom_add.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lista_medioCom_addMouseClicked(evt);
            }
        });
        jScrollPane7.setViewportView(lista_medioCom_add);

        jLabel3.setText("Medios de Comunicación");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addContainerGap(521, Short.MAX_VALUE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addContainerGap(593, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE)
                            .addGap(117, 117, 117)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel2)
                                    .addContainerGap())
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE)
                                    .addGap(18, 18, 18)
                                    .addComponent(jScrollPane6, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE)
                                    .addGap(27, 27, 27))))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jScrollPane8, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE)
                                    .addGap(18, 18, 18)
                                    .addComponent(jScrollPane9, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE)))
                            .addGap(117, 117, 117)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE)
                                    .addGap(18, 18, 18)
                                    .addComponent(jScrollPane7, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE)
                                    .addGap(27, 27, 27))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel3)
                                    .addContainerGap()))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btn_agregar)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(20, 20, 20)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(45, 45, 45)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3))
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(39, 39, 39)
                .addComponent(btn_agregar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        label1.getAccessibleContext().setAccessibleName("");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void list_leftMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_list_leftMousePressed
        // TODO add your handling code here:
        Musica musica = (Musica)list_left.getSelectedValue();
        modeloMusica.removeElement(musica);
        modeloMusicaPreferida.addElement(musica);
    }//GEN-LAST:event_list_leftMousePressed

    private void list_addMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_list_addMouseClicked
        Musica musica = (Musica)list_add.getSelectedValue();
        modeloMusicaPreferida.removeElement(musica);
        modeloMusica.addElement(musica);
    }//GEN-LAST:event_list_addMouseClicked

    private void lista_bebida_addMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lista_bebida_addMouseClicked
        // TODO add your handling code here:
        Bebida bebida = (Bebida)lista_bebida_add.getSelectedValue();
        modeloBebidaPreferida.removeElement(bebida);
        modeloBebida.addElement(bebida);
    }//GEN-LAST:event_lista_bebida_addMouseClicked

    private void list_diaSal_leftMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_list_diaSal_leftMouseClicked
        // TODO add your handling code here:
        DiaSalida diaSalida = (DiaSalida)list_diaSal_left.getSelectedValue();
        modeloDiasSalida.removeElement(diaSalida);
        modeloDiasSalidaPreferida.addElement(diaSalida);
    }//GEN-LAST:event_list_diaSal_leftMouseClicked

    private void list_diaSal_addMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_list_diaSal_addMouseClicked
        // TODO add your handling code here:
        DiaSalida diaSalida = (DiaSalida)list_diaSal_add.getSelectedValue();
        modeloDiasSalidaPreferida.removeElement(diaSalida);
        modeloDiasSalida.addElement(diaSalida);
    }//GEN-LAST:event_list_diaSal_addMouseClicked

    private void list_bebida_leftMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_list_bebida_leftMousePressed
                                       
        Bebida bebida = (Bebida)list_bebida_left.getSelectedValue();
        modeloBebida.removeElement(bebida);
        modeloBebidaPreferida.addElement(bebida);

    }//GEN-LAST:event_list_bebida_leftMousePressed

    private void btn_agregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_agregarActionPerformed
        if(validarSiguiente()==true){
            for(int i = 0;i<modeloBebidaPreferida.size();i++){
                socio.getPreferenciaBebida().add((Bebida)modeloBebidaPreferida.elementAt(i));
            }
            for(int i = 0;i<modeloDiasSalidaPreferida.size();i++){
                socio.getPreferenciaDias().add((DiaSalida)modeloDiasSalidaPreferida.elementAt(i));
            }
            for(int i = 0;i<modeloMusicaPreferida.size();i++){
                socio.getPreferenciaMusica().add((Musica)modeloMusicaPreferida.elementAt(i));
            }
            for(int i = 0;i<modeloComunicacionPreferida.size();i++){
                socio.getPreferenciaComunicacion().add((MedioComunicacion)modeloComunicacionPreferida.elementAt(i));
            }

                boolean exito = PersonaDao.saveOrUpdate(socio);
                if(exito==true){
                JOptionPane.showMessageDialog(null, "Socio guardado!", "Aviso", JOptionPane.OK_OPTION+JOptionPane.INFORMATION_MESSAGE);
            }else{
                JOptionPane.showMessageDialog(null, "No se pudo guardar el socio!", "Error", JOptionPane.OK_OPTION+JOptionPane.ERROR_MESSAGE);
            }

            int eleccion = JOptionPane.showOptionDialog(null,"Desea ingresar amigos?", "Selector de opciones",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE,null, new Object[] {"Si, agregar amigos", "no, guardar el socio" }, "Si, agregar amigos");

            if(eleccion==0){
                CargaAmigo ca = new CargaAmigo();
                this.setVisible(false);
                ca.setVisible(true);
            }else{
                this.setVisible(false);
            }
        }
    }//GEN-LAST:event_btn_agregarActionPerformed

    private void list_medioCom_leftMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_list_medioCom_leftMousePressed
        MedioComunicacion medioCom = (MedioComunicacion)list_medioCom_left.getSelectedValue();
        modeloComunicacion.removeElement(medioCom);
        modeloComunicacionPreferida.addElement(medioCom);

    }//GEN-LAST:event_list_medioCom_leftMousePressed

    private void lista_medioCom_addMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lista_medioCom_addMouseClicked
        MedioComunicacion medioCom = (MedioComunicacion)lista_medioCom_add.getSelectedValue();
        modeloComunicacionPreferida.removeElement(medioCom);
        modeloComunicacion.addElement(medioCom);
    }//GEN-LAST:event_lista_medioCom_addMouseClicked

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PreferenciaSocio().setVisible(true);
            }
        });
    }
    private DefaultListModel modeloMusica = new DefaultListModel();
    private DefaultListModel modeloMusicaPreferida = new DefaultListModel();

    private DefaultListModel modeloBebida = new DefaultListModel();
    private DefaultListModel modeloBebidaPreferida = new DefaultListModel();

    private DefaultListModel modeloDiasSalida = new DefaultListModel();
    private DefaultListModel modeloDiasSalidaPreferida = new DefaultListModel();

    private DefaultListModel modeloComunicacion = new DefaultListModel();
    private DefaultListModel modeloComunicacionPreferida = new DefaultListModel();
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_agregar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JScrollPane jScrollPane7;
    private javax.swing.JScrollPane jScrollPane8;
    private javax.swing.JScrollPane jScrollPane9;
    private java.awt.Label label1;
    private java.awt.Label label2;
    private javax.swing.JList list_add;
    private javax.swing.JList list_bebida_left;
    private javax.swing.JList list_diaSal_add;
    private javax.swing.JList list_diaSal_left;
    private javax.swing.JList list_left;
    private javax.swing.JList list_medioCom_left;
    private javax.swing.JList lista_bebida_add;
    private javax.swing.JList lista_medioCom_add;
    // End of variables declaration//GEN-END:variables


    private boolean validarSiguiente(){
        boolean valida = false;
        if(modeloMusicaPreferida.isEmpty()){
           JOptionPane.showMessageDialog(null, "Debe ingresar música preferida!", "Error", JOptionPane.OK_OPTION);
        }else if(modeloBebidaPreferida.isEmpty()){
           JOptionPane.showMessageDialog(null, "Debe ingresar por lo menos una bebida preferida!", "Error", JOptionPane.OK_OPTION);
        }else if(modeloDiasSalidaPreferida.isEmpty()){
           JOptionPane.showMessageDialog(null, "Debe ingresar por lo menos un día de salidas!", "Error", JOptionPane.OK_OPTION);
        }else if(modeloComunicacionPreferida.isEmpty()){
           JOptionPane.showMessageDialog(null, "Debe ingresar por lo menos un medio de comunicación!", "Error", JOptionPane.OK_OPTION);
        } else {
            valida = true;
        }

        return valida;
    }
}
